<?php

class SiteHelperController extends Controller{
    public function actionUpdateCounty(){
        echo 'Begin Update County';
        $locationList=Yii::app()->db->createCommand('select * from tbl_locations')->queryAll();
        foreach($locationList as $location){
            $zipcode=$location['zip'];
            if(strlen($zipcode)>5){
                $zipcode=substr($zipcode, 0,5);
            }
            $county=Yii::app()->db->createCommand('select county from tbl_taxcode where zipcode=:zipcode')
                    ->bindValue('zipcode', $zipcode)->queryScalar();
            if($county==NULL || $county===false){
                echo "County find county at location "+ $location['id'];
                Yii::log('Update county problem', 'warning');
            }
            $rs=Yii::app()->db->createCommand()->update('tbl_locations',array(
                'county'=>$county
            ), 'id=:id', array('id'=>$location['id']));
        }
        echo 'End Update County';
    }
    public function actionGetInventoryCSV(){
        $rs='';
       
        $handleOpen=fopen('protected/data/QSI.CSV', 'r+');
        $delimiter='|';
        if($handleOpen!==false){
            while($data=  fgetcsv($handleOpen)){
                $inv=Yii::app()->db->createCommand('select partno,description,averagecost from tbl_inventory where partno=:partno')
                        ->bindValue('partno', $data[0])->queryRow();
                if($inv!=NULL){
                    $total=$data[1] * $inv['averagecost'];
                    $rs.=$data[0].$delimiter.$data[1].$delimiter.$inv['description'].$delimiter.$inv['averagecost'].$delimiter.$total.PHP_EOL;
                }
            }
        }
        fclose($handleOpen);
        $writefile='protected/data/QSI-result.csv';
//        $handleWrite=fopen($writefile,'w' );
//        if($handleWrite!==false){
//            $
//        }
        //fclose($handleWrite);
        Yii::app()->request->sendFile($writefile, $rs);
    }
    public function actionCreateInvoiceForScheduleContract($contractId=null,$custno=null){
        $crit=new CDbCriteria();
        $crit->addCondition('billtype=:type1 or billtype=:type2');
        $crit->params=array(
            'type1' => Contracts::TYPE_MONTHLY,
            'type2' => Contracts::TYPE_QUARTERLY
                );
        if($contractId!=null){
            $crit->addColumnCondition(array(
                'contractid'=>$contractId
            ));
        }
        $crit->compare('custno', $custno);
        $contractList = Contracts::model()->findAll($crit);
        if(count($contractList)>0){
            foreach ($contractList as $contract)
            {
                $payment = $contract->getOnTimePayment();
                if ($payment !== false)
                {
                    $isCreatedByCron=true;
                    $invoiceNum=$contract->createScheduleInvoice($payment,$isCreatedByCron);          
                    echo $invoiceNum.'<br/>';
                }
            }
        }
    }
}
?>
